/*
 * @lc app=leetcode.cn id=350 lang=typescript
 *
 * [350] 两个数组的交集 II
 */

// @lc code=start
function intersect(nums1: number[], nums2: number[]): number[] {
    let cache = new Map<number, number>();
    nums2.forEach((i) => {
        cache.set(i, (cache.get(i) || 0) + 1);
    });
    return nums1.filter((i) => {
        const count = cache.get(i);
        if (count && count > 0) {
            cache.set(i, count - 1);
            return true;
        }
        return false;
    });
}
// @lc code=end
